import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/models/assessment/assessmentRecordsResult.dart';
import 'package:ichinesereader/models/assessment/assessmentTeacherQueryDto.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/buttons/LinkedLabelCheckbox.dart';
import 'package:ichinesereader/widgets/grids/assessmentsRecordsDataGrid.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import '../../themes/appTheme.dart';

class TeacherReportsAssessment extends StatefulWidget {
  TeacherReportsAssessment({Key? key}) : super(key: key);
  static const String routeName = '/teacher/reports/assessment';

  @override
  _TeacherReportsAssessment createState() => _TeacherReportsAssessment();
}

class _TeacherReportsAssessment extends State<TeacherReportsAssessment> {
  final ReportsController _reportsController = Get.find();
  var box = Hive.box('appconfig');
  List<String> status = [];
  String classId = '';

  @override
  void initState() {
    classId = box.get(CacheManagerKey['currentclass'].toString());
    if (_reportsController.teacherAssessmentRecord.value.list != null &&
        _reportsController.teacherAssessmentRecord.value.list!.isNotEmpty) {
      _reportsController.teacherAssessmentRecord.value.list!.clear();
    }
    Future.delayed(Duration(milliseconds: 200), () {
      AssessmentTeacherQueryDto dto =
          new AssessmentTeacherQueryDto(status: [], type: []);
      _reportsController.fetchTeacherAssessmentRecords(classId, dto);
    });
    super.initState();
  }

  void fetchData(classId, status) {
    AssessmentTeacherQueryDto dto =
        new AssessmentTeacherQueryDto(status: status, type: []);
    _reportsController.fetchTeacherAssessmentRecords(classId, dto);
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build teacher Assessment progress report');

    return Scaffold(
        backgroundColor: Colors.transparent,
        appBar: CustomAppBar(title: 'Assessment Reports'.tr),
        body: Column(children: [
          Container(
            margin: EdgeInsets.only(bottom: 12.0),
            padding: EdgeInsets.symmetric(vertical: 6.0),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(3.0),
              color: Get.isDarkMode
                  ? AppTheme.colors.darkTheme2
                  : Colors.white.withOpacity(0.3),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Container(
                    width: 90.0,
                    child: LabeledCheckbox(
                      label: "New",
                      value: status.contains("SUBMITTED"),
                      onChanged: (value) {
                        List<String> temp = status;
                        if (!value)
                          temp.remove("SUBMITTED");
                        else
                          temp.add("SUBMITTED");
                        setState(() {
                          status = temp;
                        });
                        fetchData(classId, temp);
                      },
                      padding: const EdgeInsets.all(1.0),
                    )),
                Container(
                    width: 130,
                    child: LabeledCheckbox(
                      label: "Processing",
                      value: status.contains("PROCESSING"),
                      onChanged: (value) {
                        List<String> temp = status;
                        if (!value)
                          temp.remove("PROCESSING");
                        else
                          temp.add("PROCESSING");
                        setState(() {
                          status = temp;
                        });
                        fetchData(classId, temp);
                      },
                      padding: const EdgeInsets.all(1.0),
                    )),
                Container(
                    width: 120,
                    child: LabeledCheckbox(
                      label: "Graded",
                      value: status.contains("GRADED"),
                      onChanged: (value) {
                        List<String> temp = status;
                        if (!value)
                          temp.remove("GRADED");
                        else
                          temp.add("GRADED");
                        setState(() {
                          status = temp;
                        });
                        fetchData(classId, status);
                      },
                      padding: const EdgeInsets.all(1.0),
                    )),
              ],
            ),
          ),
          _reportsController.obx(
            (data) {
              if (_reportsController.teacherAssessmentRecord.value.list !=
                  null) {
                return Container(
                    child: AssessmentsRecordsDataGrid(
                        _reportsController.teacherAssessmentRecord.value.list!,
                        new AssessmentTeacherQueryDto(
                            status: status, type: [])));
              } else {
                return Text('');
              }
            },
            onEmpty: Container(
                child: AssessmentsRecordsDataGrid(<QuestionRecord>[],
                    new AssessmentTeacherQueryDto(status: status, type: []))),
          )
        ]));
  }
}
